package com.jinlia.show.modules.base.system.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinlia.show.modules.base.model.vo.SysMenuVO;
import com.jinlia.show.modules.base.system.entity.SysMenu;
import com.jinlia.show.modules.base.system.entity.SysRoleMenu;
import com.jinlia.show.modules.base.system.mapper.SysMenuMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@RequiredArgsConstructor
public class SysMenuMapperService extends ServiceImpl<SysMenuMapper, SysMenu> {

    @Autowired
    SysRoleMenuMapperService sysRoleMenuMapperService;

    /**
     * 构造菜单树
     *
     * @return
     */
    public List<SysMenuVO> getTreeVO(SysMenu menu) {
        List<SysMenu> sysMenuVOList = this.lambdaQuery()
                .eq(menu.getMenuType() != null, SysMenu::getMenuType, menu.getMenuType())
                .list();
        //跟节点
        List<SysMenuVO> rootSysMenuVOList = sysMenuVOList.stream().map(SysMenuVO::fromEntity).collect(Collectors.toList());
        List<SysMenuVO> sysMenuVOList1 = buildTree(rootSysMenuVOList, 0l);
        return sysMenuVOList1;
    }

    public SysMenu add(SysMenu menu) {
        if (menu.getParentId() == null) {
            menu.setParentId(0L);
        }
        boolean save = this.save(menu);
        return menu;
    }

    /**
     * 构造父节点树
     *
     * @param listVO
     * @return
     */
    private List<SysMenuVO> buildTree(List<SysMenuVO> listVO, Long parentId) {
        Map<Long, SysMenuVO> idMap = listVO.stream().collect(Collectors.toMap(SysMenuVO::getId, i -> i));
        //认父节点
        List<SysMenuVO> rootFoldTree = new ArrayList<>();
        for (SysMenuVO foldTreeVO : listVO) {
            if (Objects.nonNull(foldTreeVO.getParentId()) && !foldTreeVO.getParentId().equals(Long.valueOf(0))) {
                SysMenuVO parentFoldTree = idMap.get(foldTreeVO.getParentId());//父元素
                if (parentFoldTree.getChildren() == null) {
                    parentFoldTree.setChildren(new ArrayList<>());
                }
                parentFoldTree.getChildren().add(foldTreeVO);//添加到父元素中
            }
            if ((parentId != null) && foldTreeVO.getId().equals(parentId)) {
                rootFoldTree.add(foldTreeVO);
            } else if (foldTreeVO.getParentId().equals(0l)) {
                //根节点
                rootFoldTree.add(foldTreeVO);
            }
        }
        return rootFoldTree;
    }

    public SysMenu updateMenu(SysMenu menu) {
        if (menu.getId() == null) {
            throw new RuntimeException("id不能为空");
        }
        if (menu.getParentId() == null) {
            menu.setParentId(0L);
        }
        this.updateById(menu);
        return null;
    }

    /**
     * 通过角色id获取菜单树
     */
    public List<SysMenuVO> getMenuTreeByRoleId(Long... roleIds) {
        List<SysRoleMenu> list = sysRoleMenuMapperService.lambdaQuery()
                .in(SysRoleMenu::getRoleId, roleIds)
                .list();
        List<SysMenu> sysMenuVOList = this.lambdaQuery()
                .in(SysMenu::getId, list.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList()))
                .list();
        //跟节点
        List<SysMenuVO> rootSysMenuVOList = sysMenuVOList.stream().map(SysMenuVO::fromEntity).collect(Collectors.toList());
        List<SysMenuVO> sysMenuVOList1 = buildTree(rootSysMenuVOList, 0l);
        return sysMenuVOList1;
    }
}
